package com.crealth;


import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
import com.crealth.db.UserDb;
import com.crealth.manager.MybatisManager;
import com.crealth.memcached.MemcachedMgr;
import com.crealth.model.UserModel;
import com.crealth.utils.Utils;

public class UpdateUserMOBMain {
	private static Logger logger = Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");
	
	
	
	public static void main(String[] args) {
		try {
			String osName = Utils.getOSName();
			logger.info("OS: " + osName);			
			Config.initConfig();
			MybatisManager.initSessionFactory();			
		} catch (Exception e) {
			errorLog.error("init config error", e);
			System.exit(0);
		}		
		
		MemcachedMgr.initMemcached();
		
		List<UserModel> userls = UserDb.selectAllUser();
		int curMonth = Calendar.getInstance().get(Calendar.MONTH);
		int curYear = Calendar.getInstance().get(Calendar.YEAR);
		for (UserModel user : userls) {
			int userID = user.getId();
			logger.info("userID: " + userID);
			Timestamp tm = user.getEntryTime();
			if (tm == null) {
				continue;
			}			
			 Calendar c1 = Calendar.getInstance();
			 c1.setTimeInMillis(tm.getTime());
			 int year = c1.get(Calendar.YEAR);
			 if (year > 2010) {
				 int m = c1.get(Calendar.MONTH);
				 int mob = (curYear-2010)*12 + curMonth - ((year-2010)*12+m);
				 UserDb.updateUserMOBByID(userID, mob);
			 }
		}		
		logger.info("----      finished    ------------");
		
	}	
	
	
}
